package com.realworld.controller;

import com.realworld.common.R;
import com.realworld.mapper.UserInfoMapper;
import com.realworld.model.dto.ArticleReq;
import com.realworld.model.pojo.Article;
import com.realworld.model.pojo.Comments;
import com.realworld.model.pojo.UserInfo;
import com.realworld.model.pojo.articleThumb;
import com.realworld.service.ArticleService;
import com.realworld.service.articleThumbService;
import com.realworld.service.commentsService;
import com.realworld.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("articles")
@Api(tags = "文章操作")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private commentsService commentsService;
    @Resource
    private articleThumbService articleThumbService;
    @Resource
    private UserInfoMapper userInfoMapper;

    @GetMapping
    public R getArticleList(@RequestBody Article article){
        List<Article> articleList = articleService.getArticleList(article);
        return R.ok(articleList);
    }

    // todo 根据条件查找
    @GetMapping("feed")
    public R getArticleInfoList(HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        List<Article> articleInfoList = articleService.getArticleInfoList(userInfo.getUid());
        return R.ok(articleInfoList);
    }

    @GetMapping("{id}")
    public R getArticleInfo(@PathVariable("id") Integer id){
        Article articleInfo = articleService.getArticleInfo(id);
        return R.ok(articleInfo);
    }

    @PostMapping
    // todo 增加的时候uid没有
    public R addArticle(@RequestBody ArticleReq articleReq,HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        return articleService.addArticle(articleReq,userInfo.getUid().toString());
    }

    @PutMapping
    public R updateArticle(@RequestBody ArticleReq articleReq,HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        String uid = userInfo.getUid().toString();
        return articleService.updateArticle(articleReq,uid);
    }

    @DeleteMapping("{artId}")
    public R deleteArticle(HttpServletRequest httpServletRequest,@PathVariable("artId")Integer artId) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        String uid = userInfo.getUid().toString();
        return articleService.deleteArticle(uid,artId);
    }

    @PostMapping("comments")
    public R addComments(@RequestBody Comments comments,HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        comments.setUid(userInfo.getUid());
        return commentsService.addComments(comments);
    }

    @GetMapping("comments")
    public R getCommentsList(@RequestParam("artId") Integer artId){
        List<Comments> commentsList = commentsService.getCommentsList(artId);
        return R.ok(commentsList);
    }
    @DeleteMapping("comments/{artId}")
    // todo 需要优化
    public R deleteComments(@PathVariable("artId") Integer artId,HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        String uid = userInfo.getUid().toString();
        return commentsService.deleteComments(artId,uid);
    }

    @PostMapping("favorite")
    public R Favorite(@RequestParam("artId") Integer artId,HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        String uid = userInfo.getUid().toString();
        articleThumbService.Favorite(artId,uid);
        return R.ok(4001,"favorite success!");
    }

    @DeleteMapping("favorite/{artId}")
    public R UnFavorite(@PathVariable("artId") Integer artId,HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        UserInfo userInfo = userInfoMapper.selectUserByUsername(claims.getSubject().toString());
        String uid = userInfo.getUid().toString();
        articleThumbService.UnFavorite(artId,uid);
        return R.ok(4001,"delete success!");
    }


}
